package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IPublishTalkView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class PublishTalkPresenter
{
    private BaseBean baseBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private  IPublishTalkView publishTalkView;
    private Context context;
    private BaseView baseView;
    public PublishTalkPresenter(Context context, IPublishTalkView publishTalkView, BaseView baseView)
    {
        this.context = context;
        this.publishTalkView = publishTalkView;
        this.baseView=baseView;
    }

    /**
     * 发表话题
     */
    public void publishTalk()
    {
        if("".equals(publishTalkView.getTitles())){
            Toast.makeText(context,"请填写标题",Toast.LENGTH_SHORT).show();
        }else if("".equals(publishTalkView.getContent())){
            Toast.makeText(context,"请填写话题内容",Toast.LENGTH_SHORT).show();
        }else if("".equals(publishTalkView.getType_id())){
            Toast.makeText(context,"请选择球类",Toast.LENGTH_SHORT).show();
        }else {
            baseView.showLoading();
            MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
            myOkHttpClient.addbbs(UrlData.BASEURL + UrlData.BBSADD, SharePrefencesUtil.getUser_id(context),
                    publishTalkView.getTitles(), publishTalkView.getContent(),publishTalkView.getType_id(), handler);
        }
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            baseView.hideLoading();
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("result", result);
                        baseBean=gson.fromJson(result,BaseBean.class);
                        if(0==baseBean.getState()){

                            Toast.makeText(context,"发表成功",Toast.LENGTH_SHORT).show();
                            publishTalkView.callback();
                            publishTalkView.finishActivity();
                        }else{
                            Toast.makeText(context,baseBean.getMsg(),Toast.LENGTH_SHORT).show();

                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.showError(result);
                        break;
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    };
}